import http from '../../apis/api.js';
import map from '../../utils/map.js';
import util from '../../utils/util.js';
const app = getApp();
let self;
Page({
  data: {
    search:{
      addr:'',
      filter:''
    },
    isShow:false,/**是否显示菜单 */
    storeList:[],
    hidden:false,
    activity:null,
    pagination:{
      page:1,
      limit:6,
      more:0
    },
    loading:false
  },
  //事件处理函数
  bindViewTap: function() {
  },
  onLoad: function (options) {
    self=this;
    self.data.search.addr = options.addr ? options.addr:'';
    self.data.search.filter = options.filter ? decodeURIComponent(options.filter): '';
    http.getHomepageAc().then(res=>{
      if (res.data){
        self.data.activity = res.data;
        self.setData({
          activity: self.data.activity
        })
      }
     
    })
    self.setData({
      search: self.data.search
    })
  },
  onShow(){
    self.data.storeList = [];
    self.data.pagination = {
      page: 1,
      limit: 6,
      more: 0
    }
    if (self.data.search.addr){
      self.setData({
        'search': self.data.search
      })
      self.getList();
    }else{
      map.getLocateInfo().then(res => {
        self.data.search.addr = res.city;
        self.setData({
          'search': self.data.search
        })
        self.getList();
      })
    }
    http.getAppBanner().then(res=>{
      self.data.imgUrls=res.data;
      self.setData({
        imgUrls:self.data.imgUrls
      })
    })
   
  },
  getList(){
    let data={
      addr: self.data.search.addr === "全国" ? '' : self.data.search.addr,
      page: self.data.pagination.page,
      limit: self.data.pagination.limit,
      filter: self.data.search.filter
    }
    self.setData({
      'loading': true
    })
    http.homeStayList(data).then(res=>{
      if(res.page){
        res.page.records.forEach(el=>{
          el.briefIntroFormat = util.formateContent(el.briefIntro,44);
          el.storePics.forEach(item=>{
            if (item.picType =='51000101'){
              el.thumbnail = item.inPath;
            }

          })
          self.data.storeList.push(el);
        })
        self.data.pagination.more = Number(res.page.current) * Number(res.page.size) < res.page.total ? 1 : 0;
        self.setData({
          'loading': false,
          'storeList': self.data.storeList,
          'pagination': self.data.pagination
        })
      }
     
    });
  },
  onReachBottom(){/**上拉触底时间 */
    if (self.data.pagination.more>0){
      self.data.pagination.page++;
      self.getList();
    }
  },
  tapMore(e){/**点击顶部菜单更多 */
    self.data.isShow = self.data.isShow?false:true;
    self.setData({
      'isShow':self.data.isShow
    })
  },
  handlNavigator(e){
    let data = e.currentTarget.dataset;
    let url = `/pages/${data.url}/${data.url}`;
    switch (data.url){
      case 'area':
        url;
      break;
      case 'search':
        url += `?addr=${self.data.search.addr}`;
        break;
        
      case 'detail':
        url += `?storeNo=${data.storeno}`;
        break;  
    }
    wx.navigateTo({
      url,
    })
  },
  onShareAppMessage(){
    return {
      title: '一券在手游天下',
      imageUrl: '/images/indexShare.jpg'
    }
  },
  clearSearch(){
    let self = this;
    self.data.search.filter='';
    self.setData({
      search: self.data.search
    })
    self.getList();
  },
  closeActivity(){
    self.data.hidden=true;
    self.setData({
      hidden: self.data.hidden
    })
  },
  goActivity(){
    self.data.hidden = true;
    self.setData({
      hidden: self.data.hidden
    })
    wx.navigateTo({
      url: `/pages/activityDetail/activityDetail?acNo=${self.data.activity.acNo}`,
    }) 
  }
})
